package dbcontrol

import (
	"errors"
	"reflect"
	"strconv"
)

type dbOperabase struct {
	GetData   func() interface{}
	SetData   func(interface{})
	GetKey    func() interface{}
	CreateNew func() interface{}
	CreateNil func() interface{}
}

//GetAllByPage 可分页。返回值：总页数，错误。countOfPage<=0:不分页。否则，totPage<=0时，计算分页页数
func (_this *dbOperabase) GetAllByPage(data interface{}, totPage, currentPage, countOfPage int) (int, error) {
	if data == nil || reflect.ValueOf(data).IsNil() {
		return 0, errors.New("传入查询对象为空")
	}
	conn, err := getDBContent().GetConnection()
	if err != nil || conn == nil {
		return 0, err
	}
	if countOfPage <= 0 {
		if err = conn.Find(data); err != nil {
			return 0, err
		}
		return 0, nil
	} else if totPage <= 0 {
		var num int64
		if num, err = conn.Count(data); err != nil || num <= 0 {
			return 0, err
		}
		totPage = int(num) / countOfPage
	}
	if err = conn.Limit(countOfPage, currentPage*countOfPage).Find(data); err != nil {
		return 0, err
	}
	return totPage, nil
}
func (_this *dbOperabase) FindByID(id interface{}) error {
	if id == nil || !reflect.ValueOf(id).IsValid() {
		return errors.New("传入查询ID为空")
	}
	conn, err := getDBContent().GetConnection()
	if err != nil || conn == nil {
		return err
	}
	res := _this.CreateNil()
	if isGet, err := conn.ID(id).Get(res); err != nil {
		return err
	} else if isGet {
		_this.SetData(res)
		return nil
	}
	_this.SetData(nil)
	return nil
}
func (_this *dbOperabase) FindByWhere(data interface{}, start, count int, whereString string, values ...interface{}) error {
	if whereString == "" || values == nil || len(values) == 0 {
		return errors.New("传入查询ID为空")
	}
	conn, err := getDBContent().GetConnection()
	if err != nil || conn == nil {
		return err
	}
	if count <= 0 {
		if err := conn.Where(whereString, values...).Find(data); err != nil {
			return err
		}
	} else {
		if err := conn.Where(whereString, values...).Limit(count, start).Find(data); err != nil {
			return err
		}
	}
	return nil
}
func (_this *dbOperabase) FindCountByWhere(data interface{}, whereString string, values ...interface{}) (int, error) {
	if whereString == "" || values == nil || len(values) == 0 {
		return 0, errors.New("传入查询ID为空")
	}
	conn, err := getDBContent().GetConnection()
	if err != nil || conn == nil {
		return 0, err
	}
	count, err := conn.Where(whereString, values...).Count(&data)
	if err != nil {
		return 0, err
	}
	return int(count), nil
}
func (_this *dbOperabase) Save() error {
	data := _this.GetData()
	if data == nil || reflect.ValueOf(data).IsNil() {
		return errors.New("更新对象为空")
	}
	conn, err := getDBContent().GetConnection()
	if err != nil || conn == nil {
		return err
	}
	if res, _ := _this.IsExist(_this.GetKey()); !res {
		len, err := conn.InsertOne(data)
		if err != nil {
			return err
		} else if len != 1 {
			return errors.New("插入条目错误")
		}
		return nil
	}
	if num, err := conn.ID(_this.GetKey()).Update(data); err != nil {
		return err
	} else if num != 1 {
		return errors.New("更新条目数不正确：" + strconv.FormatInt(num, 10))
	}
	return nil
}
func (_this *dbOperabase) IsExist(id interface{}) (bool, error) {
	if id == nil || !reflect.ValueOf(id).IsValid() {
		return false, errors.New("传入查询ID为空")
	}
	conn, err := getDBContent().GetConnection()
	if err != nil || conn == nil {
		return false, err
	}
	res := _this.CreateNil()
	temr, err2 := conn.ID(id).Get(res)
	return temr, err2
}
func (_this *dbOperabase) Delete() error {
	data := _this.GetData()
	if data == nil || reflect.ValueOf(data).IsNil() {
		return errors.New("删除对象为空")
	}
	conn, err := getDBContent().GetConnection()
	if err != nil || conn == nil {
		return err
	}
	if num, err := conn.ID(_this.GetKey()).Delete(data); err != nil {
		return err
	} else if num != 1 {
		return errors.New("更新条目数不正确：" + strconv.FormatInt(num, 10))
	}
	return nil
}
